local input = GLOBAL.TheInput

local function InGamePlaying()
    return InGame() and GLOBAL.ThePlayer.prefab ~= "wortox" and GLOBAL.ThePlayer:GetPosition()
    -- and not input:GetWorldEntityUnderMouse()
end

local function MoveTo(x, y)
    if x and y then
        SendActionAndFn(GLOBAL.BufferedAction(GLOBAL.ThePlayer, nil, GLOBAL.ACTIONS.WALKTO, nil, input:GetWorldPosition()), function()
            GLOBAL.SendRPCToServer(GLOBAL.RPC.DirectWalking, x, y)
        end)
    end
end


input:AddMouseButtonHandler(function(button, down, x, y)
    if button == GLOBAL.MOUSEBUTTON_MIDDLE 
    and not down
    and InGamePlaying()
    then
        local pos_player = GLOBAL.ThePlayer:GetPosition()
        local pos_screen = input:GetWorldPosition()
        MoveTo(GetDirectionAToB(pos_screen, pos_player, "up"))
    end
end)
